<?php

class ArticleApp {
	var $form;
	var $pageLocation;
	var $errors = array();
	var $resultProccessRequest = false;
	var $receiver = "igor.s.melnik@gmail.com";
	
	function ArticleApp() {
		import("net.html.form.Form");
		import("data.logic.content.Article");
	}
	
	function getList() {
		return Article::findAll();
	}
	
	function getItem($id) {
		return Article::findById($id);
	}
	
	function getForm() {
		return $this->form;
	}
	
	function setPageLocation($page) {
		$this->pageLocation = $page;
	}
	function getPageLocation() {
		return $this->pageLocation;
	}
	
	function proccessRequest($request) {
		
		if (!$request) return;
		
		$cl_name  = $request->getParameter('name');
		$cl_email  = $request->getParameter('email');
		$cl_note  = $request->getParameter('note');
		$cl_code  = $request->getParameter('code');
		
		$this->form->setValueByElement("name", $cl_name);
		$this->form->setValueByElement("email", $cl_email);
		$this->form->setValueByElement("note", $cl_note);
		
		if ($request->getMethod()=="POST") {
			import("net.html.form.check.Validator");
			$validator = new Validator($cl_name);
			$validator->validateSpaceString();
			$validator->validateEmptyString();
			if (!$validator->isValid()) {
				$this->addError("name","Incorrect data in field 'Name'");
			}
			
			$validator = new Validator($cl_email);
			$validator->validateSpaceString();
			$validator->validateEmptyString();
			if (!$validator->isValid()) {
				$this->addError("email","Incorrect data in field 'E-Mail'");
			}
			
			$validator = new Validator($cl_note);
			$validator->validateSpaceString();
			$validator->validateEmptyString();
			if (!$validator->isValid()) {
				$this->addError("note","Incorrect data in field 'Message'");
			}
			
			$validator = new Validator($cl_code);
			$validator->validateSpaceString();
			$validator->validateEmptyString();
			if (!$validator->isValid() || $_SESSION[$this->getSessionKeySecretCode()]!=$cl_code) {
				$this->addError("code","Incorrect data in field 'Security Code'");
			}
		
			if (!$this->hasErrors()) {
				$charset = "utf-8";
				import("mail.Mail");
		    	$sender = new Mail();
		    	$sender->setCharSet($charset);
		    	//$sender->setContentType("text/html");
		    	$sender->setSender("noreplay@adiodom.com");
		    	$sender->setReceiver($this->receiver);
		    	
		    	$cl_subject = "Feedback from ".$this->pageLocation;
				$cl_subject = Mail::mimeEncode($cl_subject, $charset);
	
		    	$sender->Send($cl_subject, "Author: ".$cl_name."\r\n"."E-mail: ".$cl_email."\r\n\t".$cl_note);
		    	$this->resultProccessRequest = true;
			} else {
				$this->resultProccessRequest = false;
				$_SESSION[$this->getSessionKeySecretCode()]=rand(1000,9999);
			}
		}
	}
	
	function hasErrors() {
		return count($this->errors);
	}
	
	function addError($key, $message) {
		$this->errors[$key] = $message;
	}
	
	function getErrors() {
		return $this->errors;
	}
	
	function getSessionKeySecretCode() {
		return $this->form->getName()."secret_number";
	}
	
	function assignToTemplates(&$templates) {
		//Smarty
		$templates->assign("forms",array($this->form->getName()=>$this->form->toArray()) );
		if ($this->resultProccessRequest) {
			$templates->assign("result",array($this->form->getName()=>array("message"=>"Your message has been sent")) );
		}
	}
}
?>